package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月05日 18:42
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1,获取分页参数
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        //2,开启分页
        PageHelper.startPage(page,pageSize);
        //3,调用mapper层
        Page<Category> categories = categoryMapper.page(categoryPageQueryDTO);
        //4,封装结果并返回
        long total = categories.getTotal();
        List<Category> result = categories.getResult();
        //5，返回结果
        return new PageResult(total,result);

    }

    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        //1,对象拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //2,分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        //3,设置时间，创建人
       /* category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        //4,调用mapper层
        categoryMapper.add(category);

    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //设置修改时间、修改人
       /* category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/

        categoryMapper.update(category);

    }

    @Override
    public void update(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
       /* category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());*/


        categoryMapper.update(category);
    }
    @Override
    public List<Category> selectByType(Integer type) {
       List<Category> categoryList = categoryMapper.selectByType(type);
       return categoryList;

    }

}
